package com.seekting.mynews.activity;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.StaggeredGridLayoutManager;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;

import com.android.volley.Request;
import com.bumptech.glide.Glide;
import com.bumptech.glide.RequestManager;
import com.seekting.mynews.App;
import com.seekting.mynews.R;
import com.seekting.mynews.connect.tiangou.bean.response.TianGouGallery;
import com.seekting.mynews.connect.tiangou.bean.response.TianGouList;
import com.seekting.mynews.eventbus.TianGouListEvent;
import com.seekting.mynews.manager.Action;
import com.seekting.mynews.manager.NewsManager;
import com.seekting.mynews.view.ImageAdapter;
import com.seekting.mynews.view.OnRecycleItemClickListener;
import com.seekting.util.SLog;

import java.util.ArrayList;

import de.greenrobot.event.EventBus;

public class NewsListActivity extends Activity implements OnItemClickListener, OnRecycleItemClickListener<TianGouGallery> {

    public static final String ID = "id";
    private NewsManager mNewsManager;
    private ArrayList<TianGouGallery> mList = new ArrayList<TianGouGallery>();
    private RecyclerView mListView;
    private LayoutInflater mLayoutInflater;
    private String begin;
    private boolean needRefresh = false;
    private String currentRequest;
    private int currentPage = 1;
    private String nextPageRequest;
    private String firstPageRequest;
    private String cachePageRequest;
    private boolean hasRefresh = false;
    private ImageAdapter mAdapter;
    private RequestManager mRequestManager;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        needRefresh = true;
        super.onCreate(savedInstanceState);
        mRequestManager = Glide.with(this);
        setContentView(R.layout.news_list);
        mListView = (RecyclerView) findViewById(R.id.list);
        begin = getString(R.string.tngou_img_begin);

        EventBus.getDefault().register(this);

        mNewsManager = App.getApp().getManager(NewsManager.class);

        mLayoutInflater = LayoutInflater.from(this);
        mListView.setLayoutManager(new StaggeredGridLayoutManager(3, StaggeredGridLayoutManager.VERTICAL));
        mAdapter = new ImageAdapter(this, begin, mList, mRequestManager);
        mAdapter.setOnRecycleItemClickListener(this);
        mListView.setAdapter(mAdapter);

        onRefresh();


    }

    @Override
    public void onWindowFocusChanged(boolean hasFocus) {
        super.onWindowFocusChanged(hasFocus);
        if (hasFocus && needRefresh) {
            needRefresh = false;
            //            mListView.setRefreshing();
        }
    }

    public void onEventMainThread(TianGouListEvent tianGouListEvent) {
        if (TextUtils.isEmpty(currentRequest)) {
            return;
        }
        if (tianGouListEvent != null) {
            TianGouList tianGouList = tianGouListEvent.getTianGouList();
            if (tianGouList != null && currentRequest.equals(tianGouList.getUuid())) {
                ArrayList<TianGouGallery> tngou = tianGouList.getTngou();
                int total = tianGouList.getTotal();
                SLog.d("total=" + total);

                if (tngou != null) {
                    if (currentRequest.equals(nextPageRequest)) {
                        nextPageRequest = null;

                    } else if (currentRequest.equals(firstPageRequest)) {
                        mList.clear();
                        firstPageRequest = null;
                        hasRefresh = true;
                    } else if (currentRequest.equals(cachePageRequest)) {
                        cachePageRequest = null;
                    }
                    mList.addAll(tngou);
                    mAdapter.notifyDataSetChanged();

                }

            }

        }

    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        EventBus.getDefault().unregister(this);
        mRequestManager.onDestroy();
    }

    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        position = mList.indexOf(view.getTag());
        Intent intent = new Intent(this, NewsDetailActivity.class);
        intent.putExtra(NewsDetailActivity.EXTRA_LIST, mList);
        intent.putExtra(NewsDetailActivity.EXTRA_INDEX, position);
        startActivity(intent);
    }

//    @Override
//    public void onAutoRefresh() {
//        Intent intent = getIntent();
//        int id = intent.getIntExtra(ID, 0);
//        Bundle bundle = new Bundle();
//        bundle.putInt("id", id);
//        bundle.putInt("rows", 20);
//        bundle.putInt("page", 1);
//        cachePageRequest = currentRequest = mNewsManager.request(Action.TianGouList, bundle);
//    }

    //    @Override
    public void onRefresh() {
        Intent intent = getIntent();
        int id = intent.getIntExtra(ID, 0);
        Bundle bundle = new Bundle();
        bundle.putInt("id", id);
        bundle.putInt("rows", 100);
        bundle.putInt("page", 1);
        bundle.putInt(Request.REQUEST_FROM, Request.FROM_HTTP);
        firstPageRequest = currentRequest = mNewsManager.request(Action.TianGouList, bundle);

    }

    @Override
    public void onItemClick(int position, TianGouGallery tianGouGallery) {
        Intent intent = new Intent(this, NewsDetailActivity.class);
        intent.putExtra(NewsDetailActivity.EXTRA_LIST, mList);
        intent.putExtra(NewsDetailActivity.EXTRA_INDEX, position);
        startActivity(intent);
    }


//
//    @Override
//    public void onLoadMore() {
//        currentPage++;
//        Intent intent = getIntent();
//        int id = intent.getIntExtra(ID, 0);
//        Bundle bundle = new Bundle();
//        bundle.putInt("id", id);
//        bundle.putInt("rows", 20);
//        bundle.putInt("page", currentPage);
//        if (hasRefresh) {
//            bundle.putInt(Request.REQUEST_FROM, Request.FROM_HTTP);
//            SLog.d("needRefresh");
//        }
//        nextPageRequest = currentRequest = mNewsManager.request(Action.TianGouList, bundle);
//    }

//    @Override
//    public void onItemClick(PLA_AdapterView<?> parent, View view, int position, long id) {
//        position = mList.indexOf(view.getTag());
//        Intent intent = new Intent(this, NewsDetailActivity.class);
//        intent.putExtra(NewsDetailActivity.EXTRA_LIST, mList);
//        intent.putExtra(NewsDetailActivity.EXTRA_INDEX, position);
//        startActivity(intent);
//    }
}
